<?php
namespace app\common\model;
class OpenBase extends \think\Model {
    protected $header = [];
	/**
	 * @name 正则验证方法
	 * @access public
	 * @param array $data 验证的信息,规则如下
	 *  ['验证的类型','验证的字符串','最小长度','最大长度']
	 * @return boolean 是否通过验证
	 */
	public function _check_preg($data) {
		return D('Valid')->do_check($data);
	}
	/**
	 * @name 创建指定长度的字符串code
	 * @access protected
	 * @param int $length 创建的长度
	 * @param boolean $onlynum 是否纯数字  default :false
	 * @return string 生成的字符串
	 */
	protected function _createCode($length = 6, $onlynum = false) {
		if (!$onlynum) {
			$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		} else {
			$chars = '0123456789';
		}
		$str_length = strlen($chars);
		$salt = '';
		for ($i = 0; $i < $length; $i++) {
			$salt .= substr($chars, mt_rand(0, $str_length - 1), 1);
		}
		return $salt;
	}
	/**
	 * @name 加密方法,支持自定义秘钥
	 * @access protected
	 * @param string $text 原文本
	 * @param string $salt 秘钥
	 * @return string 加密后的文本
	 */
	protected function _jiami($text, $salt = '') {
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=_";
		if (!$salt) {
			for ($i = 0; $i < 6; $i++) {
				$salt .= substr($chars, mt_rand(0, 64), 1);
				$salt = preg_replace('/[-=_]/', '', $salt);
				$i = strlen($salt);
			}
		}
		$mdKey = md5($salt);
		$mdKey = substr($mdKey, 0, 16) . $salt . substr($mdKey, 16);
		$txt = base64_encode($text);
		for ($i = 0; $i < strlen($txt); $i++) {
			$k = $k == strlen($mdKey) ? 0 : $k;
			$j = (strpos($chars, $txt[$i]) + ord($mdKey[$k++])) % 64;
			$tmp .= $chars[$j];
		}
		return $salt . $tmp;
	}
	/**
	 * @name 解密方法,支持自定义秘钥,但解密秘钥必须与加密相同
	 * @access protected
	 * @param string  $txt 加密后的文本
	 * @access int $salt 秘钥的长度
	 * @return 解密后的文本
	 */
	protected function _jiemi($txt, $salt = 6) {
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=_";
		$st = substr($txt, 0, $salt);
		$mdKey = md5($st);
		$mdKey = substr($mdKey, 0, 16) . $st . substr($mdKey, 16);
		$txt = substr($txt, $salt);
		$tmp = '';
		for ($i = 0; $i < strlen($txt); $i++) {
			$k = $k == strlen($mdKey) ? 0 : $k;
			$j = strpos($chars, $txt[$i]) - ord($mdKey[$k++]);
			while ($j < 0) {
				$j += 64;
			}

			$tmp .= $chars[$j];
		}
		return base64_decode($tmp);
	}
	/**
	 * @name 更新单个字段信息
	 * @access public
	 * @param string $field 字段
	 * @param string $value 值
	 * @param array $map 条件
	 * @return int 受影响的记录数
	 */
	public function updateField($field, $value, $map) {
		return $this->where($map)->setField($field, $value);
	}
	/**
	 * @name 清理用户缓存信息
	 * @access public
	 * @param array or int 用户uid
	 */
	public function cleancacheUser($uid = []) {
		$uids = self::_getUids($uid);
		foreach ($uids as $v) {
			S('info_user_' . $v . '_true', null);
			S('info_user_' . $v . '_false', null);
		}
	}
	/**
	 * @name 解析uids成数组格式
	 * @access public
	 * @param int|string|array 用户uid
	 * @return array 用户uids的数组格式
	 */
	protected function _getUids($uid) {
		if (!$uid) {
			return false;
		}

		if (is_array($uid) && !empty($uid)) {
			return $uid;
		} elseif (is_string($uid)) {
			return explode(',', (string) $uid);
		} else {
			return (array) $uid;
		}
	}
	/**
	 * @name 必要的参数验证
	 * @access protected
	 * @param array $data 原数据信息
	 * @param array $must 必须的参数
	 * @return boolean || array  返回true表示验证通过
	 */
	protected function _mustParam($data, $must = []) {
		if (!empty($must)) {
			$m_count = count(array_filter($must));
			$r_count = count(array_intersect_key(array_flip($must), $data));
			if ($m_count > $r_count) {
				return getApiErrorCode('40001');
			}
		}
		return true;
	}
	/**
	 * @name 调用服务发送请求操作
	 * @param string $type 请求的类型   [post,put,get,delete]
	 * @param array $params 请求时携带的参数
	 * @param string $url  请求的url
	 * @return array 请求后的返回数据
	 */
	protected function request($type = '', $params = [], $url = '') {
		if (!$type || !$url) {
			return [];
		}

		//加载httpful
		vendor('Bootstrap', VENDOR_PATH . 'Httpful' . DS);
		//初始化
		\Httpful\Bootstrap::init();
		//参数拼接
		if (in_array($type, ['get', 'delete'])) {
			$url .= '?' . http_build_query($params);
			$send = \Httpful\Request::$type($url);
		} else {
			$send = \Httpful\Request::$type($url)
				->sendsForm()
				->body($params);
		}
		foreach ($this->header as $key => $val) {
			$send = $send->$key($val);
		}
		$response = $send
			->expectsHtml()
			->send();
		//数据转换为数组返回
		return object2array($response->body);
	}
}